# -*- coding: utf-8 -*-
#===================================================================================================
# Created on 23 Mar 2011
# 
# @author: serj
#===================================================================================================
class SystemApi(object):
    @staticmethod
    def listMethods(server):
        '''
        Returns an array of method names supported by the server. 
        @param server: xmlrpclib.ServerProxy instance
        @return: an array of method names supported by the server
        '''
        return server.system.listMethods()
    
    @staticmethod
    def getCapabilities(server):
        '''
        This method lists all the capabilites that the XML-RPC server has: the (more or less standard) extensions to the xmlrpc spec that it adheres to
        @param server: xmlrpclib.ServerProxy instance
        @return: struct - list of capabilities, described as structs with a version number and url for the spec
        '''
        return server.system.getCapabilities()

class BloggerApi(object):
    @staticmethod
    def deletePost():
        raise NotImplementedError
    
    @staticmethod
    def editPost():
        raise NotImplementedError
    
    @staticmethod
    def getPost():
        raise NotImplementedError
    
    @staticmethod
    def getRecentPosts(server, blog_id, username, password, num_result=50, appkey=''):
        '''
        Get blog recent post
        @param server:
        @param blog_id:
        @param username:
        @param password:
        @param num_result:
        @param appkey:
        @return: an array of structs (content,postId,userId,dateCreated) 
        '''
        return server.blogger.getRecentPosts(appkey, blog_id, username, password, num_result)
    
    @staticmethod
    def getTemplate():
        raise NotImplementedError
    
    @staticmethod
    def getUsersBlogs(server, username, password, appkey=''):
        '''
        Return user's blog list
        @param server: xmlrpclib.ServerProxy instance
        @param username: user login
        @param password: user password
        @param appkey: currently ignored
        
        @return: array of structs containing the section (blogid), section (blogName), section (url), and section (xmlrpc) for wp
        '''
        return server.blogger.getUsersBlogs(appkey, username, password)
    
    @staticmethod
    def getUserInfo(server, username, password, appkey=''):
        '''
        Return user's info
        @param server: xmlrpclib.ServerProxy instance
        @param username: user with priveleges to post to the weblog
        @param password: user's password
        @param appkey: currently ignored
        '''
        return server.blogger.getUserInfo(appkey, username, password)
    
    @staticmethod
    def newPost():
        pass
    
    @staticmethod
    def setTemplate():
        raise NotImplementedError

def MovableTypeApi(object):
    @staticmethod
    def getCategoryList():
        raise NotImplementedError
    
    @staticmethod
    def getPostCategories(server, username, password, post_id):
        return server.mt.getPostCategories(post_id, username, password)
    
    @staticmethod
    def setPostCategories():
        raise NotImplementedError
    
    @staticmethod
    def supportedMethods():
        raise NotImplementedError
    
    @staticmethod
    def supportedTextFilters():
        raise NotImplementedError
    
class MetaWeblogApi(object):
    @staticmethod
    def editPost(server, post_id, username, password, title, description, categories, publish):
        '''
        Edit already created post
        @param server: xmlrpclib.ServerProxy instance
        @param post_id: unique article ID
        @param username: user priveleged to post to the weblog
        @param password: user's password
        @param title: post title
        @param description: post content
        @param categories: post categories
        @param publish: save the post as published
        @return: true on success of error if operation fails
        '''
        return server.metaWeblog.editPost(post_id,
                                          username,
                                          password,{
                                                'title': title,
                                                'description': description,
                                                'categories': categories},
                                          publish)
    
    @staticmethod
    def getCategories(server, blog_id, username, password):
        '''
        Get blog category list
        @param server: xmlrpclib.ServerProxy instance
        @param blog_id: unique blog ID
        @param username: user privileged to post to the weblog
        @param password: user's password
        @return: array of structs(category)
        '''
        return server.metaWeblog.getCategories(blog_id, username, password)
    
    @staticmethod
    def getPost():
        raise NotImplementedError
    
    @staticmethod
    def getRecentPosts(server, blog_id, username, password, num_result=50):
        '''
        Get blog recent post
        @param server: xmlrpclib.ServerProxy instance
        @param blog_id: unique blog ID
        @param username: user privileged to post to the weblog
        @param password: user's password
        @param num_result: maximum number of result
        @return: an array of structs (content,postId,userId,dateCreated) 
        '''
        return server.metaWeblog.getRecentPosts(blog_id, username, password, num_result)
    
    @staticmethod
    def newPost(server, blog_id, username, password, title, description, categories, publish):
        '''
        Create new post
        @param server: xmlrpclib.ServerProxy instance
        @param blog_id: string ID of the blog
        @param username: user with priveleges to post to the weblog
        @param password: user's password
        @param title: post title
        @param description: post content
        @param categories: array of categories string ID
        @param publish: save the article as published
        @return: the new post ID
        '''
        return server.metaWeblog.newPost(blog_id,
                              username,
                              password, {
                                 'title': title,
                                 'description': description,
                                 'categories': categories
                              },
                              publish)
